package com.example.blemulticondemo.utils;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 通用定时器
 */
public class MyReceiveTimer {

    private static MyReceiveTimer myTimer;
    //超时处理
    public Timer mTimer = null;
    public TimerTask mTimerTask = null;

    public static MyReceiveTimer getInstance() {
        if (myTimer == null) {
            myTimer = new MyReceiveTimer();
        }
        return myTimer;
    }

    /**
     * 开始测试超时处理
     */
    public void setTimerStart(final TimerOutCallBack callBack, int timeOut) {
        setTimerCancel();
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                callBack.timerOver();
            }
        };
        mTimer.schedule(mTimerTask, timeOut);
    }

    /**
     * 取消测试超时处理
     */
    public void setTimerCancel() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        if (mTimerTask != null) {
            mTimerTask.cancel();
            mTimerTask = null;
        }
    }


    public interface TimerOutCallBack {
        void timerOver();
    }


}
